# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 10:16:53 2019
把坐标标签在图上画出来
@author: ikaros
"""

import cv2
import os
import numpy as np


root_img = './data/dataset/mlt/image/'    # 图片存放目录
root_label = './data/dataset/mlt/label/'  # 标签存放目录，txt文件
save_dir = './check/'
img_files = np.array(os.listdir(root_img))
label_files = np.array(os.listdir(root_label))
img_count = img_files.shape[0]
imgs = []
labels = []
for a1 in range(img_count):
#    imgs.append(root_img + img_files[a1])
#    labels.append(root_label + label_files[a1])
	# 图片与标签文件的名字是一一对应的
    img_dir = root_img + img_files[a1]  
    label_dir = root_label + label_files[a1]

    img = cv2.imread(img_dir)
    img2 = img.copy()
    txt = label_dir

    with open(txt,"r") as f: 
        txt2 = f.read()
    txt3 = txt2.split('\n')   # 按行切分
    num = 0 #查看程序有无卡死
    for i in txt3: 
        print(num)
        num = num+1
        if i == '':
            break
        txt4 = i.split(',')    
        a = np.array(txt4).shape
        print(a)
        if a[0] == 8:  # 数据格式为4个点的情况
            x1 = np.array(txt4[0]) # xmin
            y1 = np.array(txt4[1]) # ymin
            x2 = np.array(txt4[4]) # xmax
            y2 = np.array(txt4[5]) # ymax
        else:          # 数据格式为2个点的情况，即对角线
            x1 = np.array(txt4[0]) # xmin
            y1 = np.array(txt4[1]) # ymin
            x2 = np.array(txt4[2]) # xmax
            y2 = np.array(txt4[3]) # ymax
        cv2.rectangle(img2,(x1,y1),(x2,y2),(0,255,0),2)
    
    save_img = save_dir + 'x'+ img_files[a1]
    cv2.imwrite(save_img,img2)
#cv2.imshow('img',img2)
#print(txt4)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
